package org.example.Interact;

import org.example.Entity.Product;
import org.example.Service.ProductService;

import java.util.List;
import java.util.Scanner;

/**
 * 交互管理类，处理用户与商品管理系统的交互操作。
 */
public class InteractionManager {
    private final ProductService productService;

    public InteractionManager(ProductService productService) {
        this.productService = productService;
    }

    /**
     * 显示商品管理系统菜单。
     */
    public void displayMenu() {
        System.out.println("====== 商品管理系统 ======");
        System.out.println("1. 添加商品");
        System.out.println("2. 查看所有商品");
        System.out.println("3. 根据ID查看商品");
        System.out.println("4. 根据名称查看商品");
        System.out.println("5. 根据价格范围查看商品");
        System.out.println("6. 删除商品");
        System.out.println("7. 修改商品名称");
        System.out.println("8. 修改商品价格");
        System.out.println("9. 修改商品库存数量");
        System.out.println("0. 退出");
        System.out.print("请输入操作编号: ");
    }

    /**
     * 处理用户选择的操作。
     *
     * @param choice  用户选择的操作编号。
     * @param scanner 用于接收用户输入的Scanner对象。
     */
    public void processChoice(int choice, Scanner scanner) {
        switch (choice) {
            case 1:
                addProduct(scanner);
                break;
            case 2:
                showAllProducts();
                break;
            case 3:
                getProductById(scanner);
                break;
            case 4:
                getProductByName(scanner);
                break;
            case 5:
                getProductsByPriceRange(scanner);
                break;
            case 6:
                deleteProductById(scanner);
                break;
            case 7:
                updateProductName(scanner);
                break;
            case 8:
                updateProductPrice(scanner);
                break;
            case 9:
                updateProductStockQuantity(scanner);
                break;
            case 0:
                System.out.println("感谢使用，再见！");
                System.exit(0);
            default:
                System.out.println("无效操作，请重新输入");
        }
    }

    private void addProduct(Scanner scanner) {
        System.out.println("请输入商品信息（id,name,price,stock_quantity）:");
        String[] input = scanner.nextLine().split(",");
        Product product = new Product(input[0], input[1], Double.parseDouble(input[2]), Integer.parseInt(input[3]));
        productService.addProduct(product);
        System.out.println("商品添加成功！");
    }

    private void showAllProducts() {
        List<Product> products = productService.getAllProducts();
        if (products.isEmpty()) {
            System.out.println("暂无商品信息");
        } else {
            System.out.println("所有商品信息:");
            for (Product product : products) {
                System.out.println(product);
            }
        }
    }

    private void getProductById(Scanner scanner) {
        System.out.print("请输入商品ID: ");
        String id = scanner.nextLine();
        Product product = productService.getProductById(id);
        if (product != null) {
            System.out.println("商品信息:" + product);
        } else {
            System.out.println("未找到该商品");
        }
    }

    private void getProductByName(Scanner scanner) {
        System.out.print("请输入商品名称: ");
        String name = scanner.nextLine();
        Product product = productService.getProductByName(name);
        if (product != null) {
            System.out.println("商品信息:" + product);
        } else {
            System.out.println("未找到该商品");
        }
    }

    private void getProductsByPriceRange(Scanner scanner) {
        System.out.print("请输入价格范围（格式：min->max）: ");
        String[] range = scanner.nextLine().split("->");
        double minPrice = Double.parseDouble(range[0]);
        double maxPrice = Double.parseDouble(range[1]);

        List<Product> products = productService.getProductsByPriceRange(minPrice, maxPrice);
        if (products.isEmpty()) {
            System.out.println("未找到符合条件的商品");
        } else {
            System.out.println("符合条件的商品信息:");
            for (Product product : products) {
                System.out.println(product);
            }
        }
    }

    private void deleteProductById(Scanner scanner) {
        System.out.print("请输入要删除的商品ID: ");
        String id = scanner.nextLine();
        productService.deleteProductById(id);
        System.out.println("商品删除成功！");
    }

    private void updateProductName(Scanner scanner) {
        System.out.print("请输入要修改的商品ID: ");
        String id = scanner.nextLine();
        System.out.print("请输入新的商品名称: ");
        String newName = scanner.nextLine();
        productService.updateProductName(id, newName);
        System.out.println("商品名称修改成功！");
    }

    private void updateProductPrice(Scanner scanner) {
        System.out.print("请输入要修改的商品ID: ");
        String id = scanner.nextLine();
        System.out.print("请输入新的商品价格: ");
        double newPrice = scanner.nextDouble();
        productService.updateProductPrice(id, newPrice);
        System.out.println("商品价格修改成功！");
    }

    private void updateProductStockQuantity(Scanner scanner) {
        System.out.print("请输入要修改的商品ID: ");
        String id = scanner.nextLine();
        System.out.print("请输入新的商品库存数量: ");
        int newStockQuantity = scanner.nextInt();
        productService.updateProductStockQuantity(id, newStockQuantity);
        System.out.println("商品库存数量修改成功！");
    }
}
